package cn.zhihong.service.impl;

import cn.zhihong.common.CustomException;
import cn.zhihong.pojo.Dish;
import cn.zhihong.pojo.Setmeal;
import cn.zhihong.service.DishService;
import cn.zhihong.service.SetmealService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.zhihong.pojo.Category;
import cn.zhihong.service.CategoryService;
import cn.zhihong.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author 86183
* @description 针对表【category(菜品及套餐分类)】的数据库操作Service实现
* @createDate 2022-04-22 12:48:42
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{
    @Autowired
    DishService dishService;
    @Autowired
    SetmealService setmealService;

    @Override
    public void myRemoveById(Long id) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        long count1 = dishService.count(dishLambdaQueryWrapper);
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        long count2 = setmealService.count(setmealLambdaQueryWrapper);
        //查询当前分类是否关联了菜品，如果已经关联，则抛出一个业务异常
        if (count1 > 0){
            throw new CustomException("当前分类已关联菜品，不能删除");
        }
        //查询当前分类是否关联了套餐，如果已经关联，则抛出一个业务异常
        if (count2 > 0){
            throw new CustomException("当前分类已关联套餐，不能删除");
        }
        //正常删除
        super.removeById(id);
    }
}




